package com.sky.controller.admin;

import com.sky.annotation.AutoClearAllClientDishRedis;
import com.sky.annotation.AutoClearClientOneDishRedis;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController("adminDishController")
@Slf4j
@Api(tags = "菜品接口")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 添加菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation(value = "新增菜品")
    @PostMapping
    @AutoClearClientOneDishRedis
    public Result addDishWithFlavor(@RequestBody DishDTO dishDTO) {
        log.info("addDish:{}", dishDTO);
        dishService.addDishWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 条件，分页查询菜品的信息
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation(value = "分页查询菜品")
    @GetMapping("/page")
    public Result<PageResult> getDishWithFlavor(DishPageQueryDTO dishPageQueryDTO) {
        log.info("getDishWithFlavor:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.getDishWithFlavor(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation(value = "根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getDishByCategoryId( Long categoryId) {
        log.info("getDishByCategoryId:{}", categoryId);
        List<Dish> dishList = dishService.getDishByCategoryId(categoryId);
        return Result.success(dishList);
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除菜品")
    @DeleteMapping()
    @AutoClearAllClientDishRedis
    public Result deleteDishById(@RequestParam List<Long> ids)
    {
        log.info("deleteDishById:{}", ids);
        dishService.deleteDish(ids);
        return Result.success();
    }

    /**
     * 根据id获取菜品详细信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取菜品详细信息")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        log.info("getDishById:{}", id);
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     * @return
     */
    @ApiOperation(value = "修改菜品信息")
    @PutMapping()
    @AutoClearClientOneDishRedis
    public Result updateDishById(@RequestBody DishDTO dishDTO) {
        log.info("updateDishById:{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "修改菜品状态")
    @PostMapping("/status/{status}")
    @AutoClearAllClientDishRedis
    public Result updateDishStatus(@PathVariable Integer status, Long id) {
        log.info("updateDishStatus:{}, id: {}", status, id);
        dishService.uodateDishStatusById(status, id);
        return Result.success();
    }

}
